package com.xerland.tbootstrap.injection.modules;

import android.accounts.AccountManager;
import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;

import com.squareup.otto.Bus;
import com.xerland.tbootstrap.BootstrapService;
import com.xerland.tbootstrap.BootstrapServiceProvider;
import com.xerland.tbootstrap.BootstrapServiceProviderImpl;
import com.xerland.tbootstrap.core.authenticator.ApiKeyProvider;
import com.xerland.tbootstrap.core.authenticator.LogoutService;
import com.xerland.tbootstrap.core.authenticator.LogoutServiceImpl;
import com.xerland.tbootstrap.core.bus.PostFromAnyThreadBus;
import com.xerland.tbootstrap.injection.ApplicationContext;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import retrofit2.Retrofit;

@Module
public class ApplicationModule {

    private final Application application;

    public ApplicationModule(Application application) {
        this.application = application;
    }

    @Provides @NonNull
    Application providesApplication() {return application;}

    @Provides @NonNull
    @ApplicationContext
    Context providesContext() {return application;}

    @Singleton
    @Provides
    Bus provideOttoBus() {
        return new PostFromAnyThreadBus();
    }

    @Provides
    BootstrapService provideBootstrapService(Retrofit restAdapter) {
        return new BootstrapService(restAdapter);
    }

    @Provides
    BootstrapServiceProvider provideBootstrapServiceProvider(Retrofit restAdapter, ApiKeyProvider keyProvider) {
        return new BootstrapServiceProviderImpl(restAdapter,keyProvider);
    }

    @Provides
    ApiKeyProvider provideApiKeyProvider(AccountManager accountManager) {
        return new ApiKeyProvider(accountManager);
    }

    @Provides
    AccountManager provideAccountManager(@ApplicationContext Context context) {
        return AccountManager.get(context);
    }

//    @Provides
//    LogoutService provideLogoutService(@ApplicationContext Context context, AccountManager accountManager) {
//        return new LogoutServiceImpl(context, accountManager);
//    }
    @Provides
    @Singleton
    LogoutService provideLogoutService(@ApplicationContext final Context context, final AccountManager accountManager) {
        return new LogoutServiceImpl(context, accountManager);
    }

//    @Provides
//    Gson provideGson() {
//        /**
//         * GSON instance to use for all request  with date format set up for proper parsing.
//         * <p/>
//         * You can also configure GSON with different naming policies for your API.
//         * Maybe your API is Rails API and all json values are lower case with an underscore,
//         * like this "first_name" instead of "firstName".
//         * You can configure GSON as such below.
//         * <p/>
//         *
//         * public static final Gson GSON = new GsonBuilder().setDateFormat("yyyy-MM-dd")
//         *         .setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES).create();
//         */
//        return new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
//    }
//
//    @Provides
//    RestErrorHandler provideRestErrorHandler(Bus bus) {
//        return new RestErrorHandler(bus);
//    }
//
//    @Provides
//    UserAgentProvider providesUserAgentProvider(ApplicationInfo appInfo, PackageInfo packageInfo, TelephonyManager telephonyManager, ClassLoader classLoader) {
//        return new UserAgentProvider(appInfo, packageInfo, telephonyManager, classLoader);
//    }
//
//    @Provides
//    RestAdapterRequestInterceptor provideRestAdapterRequestInterceptor(UserAgentProvider userAgentProvider) {
//        return new RestAdapterRequestInterceptor(userAgentProvider);
//    }
//
//    @Provides
//    RestAdapter provideRestAdapter( RestAdapterRequestInterceptor restRequestInterceptor, Gson gson) {
//        return new Retrofit.Builder()
//                .baseUrl(Constants.Http.URL_BASE)
//                .setRequestInterceptor(restRequestInterceptor)
//                .setLogLevel(Retrofit.LogLevel.FULL)
//                .setConverter(new GsonConverter(gson))
//                .build();
//    }
}